home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Unix / ps2epsmac1.3.shar / er_echo.c next >
Encoding:
C/C++ Source or Header  |  1993-05-21  |  3.4 KB  |  85 lines

  1. /* er_echo.c      george jefferson george@mech.seas.upenn.edu 4/12/92        */
  2. /* replacement for the unix standard echo which writes to standard error     */
  3. /* rather than standard out.                                                 */
  4. /* i wrote this 'cause brain-dead csh has no way to redirect to stderr, sigh */
  5. /* if there is a better way ( besides using sh! ) i'd sure like to know..    */
  6. /* */
  7. /* accepts a -n flag to suppress a newline, just like the real echo          */
  8. /* */
  9. /* creeping featuritis...  the following flags are also recognised           */
  10. /* */
  11. /* -cat -> read from standard input, write to standard output                */
  12. /* */
  13. /* '-level n` sets the output level to  n ( default 0 )                      */
  14. /* '-off n`  sets the debug level to n ( default 0 )                         */
  15. /* output is supressed whenever the 'off' level is lower than t output level */
  16. /* examples might help....                                                   */
  17. /* er_echo -off 1 my dog has -level 2 ticks and -level 1 fleas               */
  18. /* my dog has fleas                                                          */
  19. /* er_echo -off 2 my dog has -level 2 ticks and -level 1 fleas               */
  20. /* my dog has ticks and fleas                                                */
  21. /* if combined with -cat, the _last_ occurance of each of these flags is used*/
  22. /* */
  23. /* -log outputfile -> output is _appended_ to outputfile                     */
  24. /* rather than going to stderr                                               */
  25. /* multiple -log arguments are processed until a valid one is found          */
  26. /* eg er_echo "-log /etc/passwd -log a -log b hi"                            */
  27. /* writes to "a" ( unless u r root! )                                        */
  28. #include <stdio.h>
  29. main (argc,argv)
  30.  int argc; char **argv;
  31. {
  32. int i,newline=1,iout=0,cat=0;
  33. FILE *logfile;
  34. char *byte[1],debug[3],off[3];
  35. logfile=stderr;
  36. for( i = 1 ; i < argc ; i++ )
  37.   if( strcmp(argv[i],"-log") == 0 && 
  38.      ( logfile=fopen(argv[++i],"a") ) != NULL )break;
  39. if( ( logfile ) == NULL )logfile=fopen("/dev/null","a");
  40.                                      /* we were trying to redirect, I guess */
  41.                                      /* dont want to see this message */
  42. strcpy(off,"0");strcpy(debug,"0");
  43. for( i = 1  ; i < argc ; i++ )
  44.   {
  45.     if( strcmp(argv[i],"-cat") == 0 )cat=1;
  46.     else if(strcmp(argv[i],"-level") == 0 )strcpy(debug,argv[++i]);
  47.     else if(strcmp(argv[i],"-off") == 0 )strcpy(off,argv[++i]);
  48.   }
  49. if( cat == 1 && strcmp(off,debug) >= 0 )  /*write input to output */
  50.   {
  51.     while(i=read(0,byte,1) != 0 )
  52.       {
  53.     fprintf(logfile,"%s",byte); 
  54.       }
  55.     exit(0);
  56.   }
  57. else if( cat == 1)  /* just dump input */
  58.   {
  59.     while(i=read(0,byte,1) != 0 );  
  60.     exit(0);
  61.   }
  62. else  /* write command line arguments to output */
  63.   {
  64.     strcpy(off,"0");strcpy(debug,"0");
  65.     for( i = 1 ; i < argc ; i++ )
  66.       {
  67.         if(strcmp(argv[i],"-n") == 0 )newline=0;
  68.         else if(strcmp(argv[i],"-off") == 0 )strcpy(off,argv[++i]);
  69.         else if(strcmp(argv[i],"-level") == 0 )strcpy(debug,argv[++i]);
  70.         else if(strcmp(argv[i],"-cat") == 0 );
  71.         else if(strcmp(argv[i],"-log") == 0 )i++;
  72.         else if(strcmp(off,debug) >= 0 )
  73.     {
  74.             if(iout++!=0)fprintf(logfile," ");
  75.             fprintf(logfile,"%s",argv[i]);
  76.           }
  77.       }
  78.     if(newline != 0 && iout != 0 )fprintf(logfile,"\n");
  79.     exit(0);
  80.   }
  81. }
  82.  
  83.  
  84.                                           
  85.